package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Article;
import com.hs.service.ArticleService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ArticleController
 * @Description TODO
 * @Author libowen
 * @Date 2025年08月01日 10:50
 * @Version 1.0
 */
@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public R addArticle(@RequestBody Article article) {
        int i = articleService.addArticle(article);
        if (i > 0) {
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @PutMapping
    public R updateArticle(@RequestBody Article article) {
        int i = articleService.updateArticle(article);
        if (i > 0) {
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") int id) {
        int i = articleService.deleteArticleById(id);
        if (i > 0) {
            return R.ok();
        }else{
            return R.fail();
        }
    }
    @DeleteMapping("deleteByIds")
    public R deleteByIds(int[] ids) {
        int i = articleService.deleteArticleByIds(ids);
        if (i > 0) {
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @GetMapping("{id}")
    public R getArticleById(@PathVariable("id") int id) {
        Article article = articleService.getArticleById(id);
        return R.ok(article);
    }

    @GetMapping("page")
    public R getArticleByPage(Article article,
                              @RequestParam(required = false,defaultValue = "1") int pageNum,
                              @RequestParam(required = false,defaultValue = "3") int pageSize) {
        PageInfo<Article> pageInfo = articleService.getAllArticle(article, pageNum, pageSize);
        return R.ok(pageInfo);
    }



}
